package com.hs.controller;

import com.hs.bean.Dept;
import com.hs.service.DeptService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
*
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
*/
@Controller
@RequestMapping("/resource/demo1")
public class DeptController {
    @Resource
    private DeptService deptService;

    @RequestMapping("/all")
    public String All(Model model){
        List<Dept> depts = deptService.QueryAll();
        model.addAttribute("depts",depts);
        return "resource/demo1/left";
    }
    @RequestMapping("detail")
    public String detail(Integer id,Model model){

        Dept dept = deptService.QueryById(id);
        String s = deptService.QueryHigh(dept.getPId());
        model.addAttribute("highName",s);
        model.addAttribute("dept",dept);
        return "resource/demo1/view";
    }
    @RequestMapping("/addbefore")
    public String addbefore(Integer pid,Model model){
        String s = deptService.QueryHigh(pid);
        model.addAttribute("pid",pid);
        model.addAttribute("s",s);
        return "resource/demo1/add";
    }
    @RequestMapping("/add")
    public String add(Dept dept){
        dept.setCreateDate(new Date());
        dept.setLevel(2);
        System.out.println(dept);
        try {
            deptService.add(dept);
            return "redirect:/resource/demo1/all";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @RequestMapping("/deptnum")
    public void find(Integer did, HttpServletResponse response)throws Exception{
        Integer deptnum = deptService.findDeptnum(did);
        if (deptnum!=null){
            response.getWriter().write("1");
        }else {
            response.getWriter().write("0");
        }
    }
    @RequestMapping("/updatebefore")
    public String before(Integer id,Model model){
        Dept dept = deptService.QueryById(id);
        String deptname = deptService.QueryHigh(dept.getPId());
        model.addAttribute("deptname",deptname);
        model.addAttribute("dept",dept);
        return "resource/demo1/update";
    }
    @RequestMapping("/update")
    public String update(Dept dept){
        System.out.println(dept);
        try {
            deptService.update(dept);
            return "redirect:/resource/demo1/all";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @RequestMapping("/delete")
    public void delete(Integer id){
        deptService.delete(id, 0);
    }
}
